package tech.aistar.corejava.test.thread.callable;

import java.util.concurrent.*;

/**
 * @author success
 * @version 1.0
 * @description:本类用来演示:
 * @date 2019/8/3 0003
 */
public class Task {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        Ta ta = new Ta();

       // Future<Integer> result =  executorService.submit(ta);
        FutureTask<Integer> result = new FutureTask<Integer>(ta);
        executorService.submit(result);
        executorService.shutdown();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("主线程执行完成!");
        try {
            System.out.println(result.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}

class Ta implements Callable<Integer>{

    @Override
    public Integer call() throws Exception {
        System.out.println("子线程正在执行...");
        Thread.sleep(1000);
        int sum = 0 ;
        for (int i = 0; i < 100; i++) {
            sum+=i;
        }
        return sum;
    }
}
